//
//  HMPhotoSelectorViewController.swift
//  图片浏览器
//
//  Created by Apple on 16/7/31.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

/// 照片选择控制器
class HMPhotoSelectorViewController: UICollectionViewController {
    /// collectionView要显示的图片
    var photos: [UIImage] = [UIImage]()
    
    /// 最大张数
    private let maxPhotoCount = 6
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()

        setupCollectionView()
    }

    private func setupCollectionView() {
        // Register cell classes
        self.collectionView!.registerClass(HMPhotoCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        
        self.layout.itemSize = CGSize(width: 80, height: 80)
        self.collectionView?.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // 当图片小于最大张数时,显示图片的数量+1(加号按钮)
        // 当图片数量等于最大张数,显示图片数量
        let count = photos.count == maxPhotoCount ? photos.count : photos.count + 1
        return count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMPhotoCell
    
        cell.backgroundColor = UIColor.brownColor()
        
        // 设置图片内容
        if indexPath.item < photos.count {
            // 有图片,获取图片
            let image = photos[indexPath.item]
            cell.image = image
        } else {
            // 要显示加号按钮
            cell.setupAddButton()
        }
        
        // 设置代理
        cell.delegate = self
    
        return cell
    }
}

extension HMPhotoSelectorViewController: HMPhotoCellDelegate {
    /// 删除按钮被点击
    func photoCellDidClickDeleteButton(cell: HMPhotoCell) {
        // 删除图片
        // 获取点击删除按钮 对应 cell 的位置
        let indexPath = collectionView!.indexPathForCell(cell)!
        
        // 去数组中删除对应图片
        photos.removeAtIndex(indexPath.item)
        
        // 刷新数据
        self.collectionView?.reloadData()
    }
    
    /// 加号按钮被点击
    func photoCellDidClickAddButton(cell: HMPhotoCell) {
        // 弹出系统的照片选择器
        let picker = UIImagePickerController()
        
        // 设置代理
        picker.delegate = self
        
        self.presentViewController(picker, animated: true, completion: nil)
    }
}


extension HMPhotoSelectorViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    // 当实现这个代理方法后需要主动关闭这个控制器
    /// 当用户点击了一张图片后会调用
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        print("image: \(image)")
        
        let newImage = image.scaleImage()
        print("newImage: \(newImage)")

        
        // 添加图片
        photos.append(newImage)
        // collectionView刷新数据
        self.collectionView?.reloadData()
        
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}
